﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using BaseTypesLib.Model.Backgrounds;
using ExplorerInfrastructure;

namespace BaseTypesLib.Converters
{
    public class BackgroundToBrushConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            if (value is TransparentColorBackground)
            {
                TransparentColorBackground bg = value as TransparentColorBackground;
                return new SolidColorBrush(Color.FromArgb(bg.Alpha, bg.Color.R, bg.Color.G, bg.Color.B));
            }
            else if (value is SolidColorBackground)
            {
                SolidColorBackground bg = value as SolidColorBackground;
                return new SolidColorBrush(Color.FromArgb(255, bg.Color.R, bg.Color.G, bg.Color.B));
            }
            else if (value is LinearGradientBackground)
            {
                return GetGradientBrush(value as LinearGradientBackground);
            }
            else if (value is ImageBackground)
            {
                return GetImageBrush(value as ImageBackground);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private Brush GetGradientBrush(LinearGradientBackground bg)
        {
            LinearGradientBrush brush = new LinearGradientBrush();
            brush.GradientStops.Add(new GradientStop(bg.Color1, 0));
            brush.GradientStops.Add(new GradientStop(bg.Color2, 1));
            brush.StartPoint = new Point(0, 0);
            if (bg.Orientation == GradientOrientation.Horizontal)
                brush.EndPoint = new Point(1, 0);
            else
                brush.EndPoint = new Point(0, 1);

            return brush;
        }
        private Brush GetImageBrush(ImageBackground bg)
        {
            ImageBrush brush = new ImageBrush();
            String projPath = bg.ProjectPath;
            String imgPath = bg.ImagePath;

            if (projPath == null) projPath = Path.GetDirectoryName(ProjectInfo.GetInstance().Path);
            if (imgPath == null || string.IsNullOrEmpty(imgPath)) return null;
            string path = Path.Combine(projPath, imgPath).Replace("/", "\\");
            if (File.Exists(path))
            {
                Uri uri = new Uri(path, UriKind.RelativeOrAbsolute);
                brush.ImageSource = new BitmapImage(uri);
            }
            if (bg.Repeat == ImageRepeat.ScaleToFit)
            {
                brush.Stretch = Stretch.Fill;
            }
            else if (bg.Repeat == ImageRepeat.None)
            {
                brush.Stretch = Stretch.None;
            }
            else
            {
                brush.Stretch = Stretch.None;
                brush.TileMode = TileMode.Tile;
                brush.ViewportUnits = BrushMappingMode.Absolute;
                brush.Viewport = new Rect(0, 0, brush.ImageSource.Width, brush.ImageSource.Height);
            }
            switch (bg.PositionX)
            {
                case PositionX.Left:
                    brush.AlignmentX = AlignmentX.Left;
                    break;
                case PositionX.Right:
                    brush.AlignmentX = AlignmentX.Right;
                    break;
                default:
                    brush.AlignmentX = AlignmentX.Center;
                    break;
            }
            switch (bg.PositionY)
            {
                case PositionY.Top:
                    brush.AlignmentY = AlignmentY.Top;
                    break;
                case PositionY.Bottom:
                    brush.AlignmentY = AlignmentY.Bottom;
                    break;
                default:
                    brush.AlignmentY = AlignmentY.Center;
                    break;
            }

            return brush;
        }
    }
}
